<?php
    /**
     * Created by JetBrains PhpStorm.
     * User: shauan
     * Date: 17/05/13
     * Time: 22:56
     * To change this template use File | Settings | File Templates.
     */
    App::uses('AppController','Controller');
    class IndexsController extends AppController
    {
        public $helpers = array('Function','Session');
        public $uses = array('Usermgmt.User');

        public function index(){
            $this->layout = 'principal';
            $this->loadModel('Recipe');
            $recipes = $this->Recipe->find('all',array('conditions'=>array('status'=>1)));
            $this->set(compact('recipes'));
            $this->set('title_for_layout','Principal');
        }

        public function verificAccount()
        {
            $this->autoRender=false;
            $this->loadModel('Account');
            $accounts_id = $this->Account->find(
                'first',
                array(
                    'conditions'=> array(
                        'year'=>$this->request->data['Account']['year'],
                        'month'=>$this->request->data['Account']['month']
                    ),
                    'fields'=>array('Account.id'),
                )
            );
            if(count($accounts_id) > 0){
                return "1|".$accounts_id['Account']['id'];
            }
            return "0|0";
        }

        public function saveAccount()
        {
            $this->autoRender=false;
            if($this->request->is('post'))
            {
                $this->loadModel('Account');
                unset($this->request->data['Account']['id']);
                $this->request->data['Account']['balance'] = $this->request->data['Account']['income'];

                $this->Account->create();

                if($this->Account->save($this->request->data)){
                    return $this->Account->getInsertID(); //retorna o último ID inserido.
                }
                else
                {
                    return false;
                }
            }
        }

        public function removeAccount($id = null)
        {
            $this->autoRender = false;
            $this->loadModel('Account');
            $this->Account->id = $id;
            if($this->Account->exists())
            {
                $this->request->onlyAllow('post','delete');
                if($this->Account->delete())
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            else
            {
                return false;
            }
        }

        public function newItemAccount()
        {
            $this->layout=false;
            $this->loadModel('Category');
            $categores = $this->Category->find('all',array('conditions'=>array('status'=>'1')));
            $this->set(compact('categores'));
            $this->render("/Elements/inc_principal/frm_Item_Account");
        }

        public function getExpenses($category_id=null)
        {
            $this->loadModel('Expense');
            $expenses = $this->Expense->find('all',array('conditions'=>array('categorie_id'=>$category_id)));
            $this->set(compact('expenses'));
            $this->render('/Elements/inc_principal/get_expenses');

        }

        public function saveItensDespesas()
        {
            $this->autoRender=false;
            if($this->request->is('post'))
            {
                $this->loadModel('AccountsExpense');
                $this->AccountsExpense->create();
                if($this->AccountsExpense->save($this->request->data['AccountsExpense'])){
                    $this->loadModel('Account');
                    if($this->Account->save($this->request->data['Account'])){
                        return '1|'.$this->AccountsExpense->getInsertID();
                    }
                }
            }
            return '0|0';
        }

        public function listItensDespesas($id = null){
            $this->loadModel('Account');
            $this->Account->id = $id;
            if($this->Account->exists())
            {
                $accounts = $this->Account->find('all',array('conditions'=>array('Account.id '=>$id)));
                $this->set(compact('accounts'));
                $this->render('/Elements/inc_principal/list_itens_despesas');
            }
        }

        public function removeItensDespesa($id = null)
        {
            $this->autoRender = false;
            $this->loadModel('AccountsExpense');
            $this->AccountsExpense->id = $id;
            echo "<pre>";
            print_r($this->AccountsExpense);
            echo "</pre>";
            exit;
            if($this->AccountsExpense->exists())
            {
                $this->request->onlyAllow('post','delete');
                if($this->AccountsExpense->delete())
                {
                    return true;
                }
                else
                {
                    echo 'error';
                }

            }
            else
            {
                $log = $this->AccountsExpense->getDataSource()->getLog(false, false);
                debug($log);
            }
        }

        public function pagaItemDespesa(){
            $this->autoRender=false;
            if($this->request->is('post'))
            {
                $this->loadModel('AccountsExpense');
                if($this->AccountsExpense->save($this->request->data)){
                    return 1;
                }
                return 0;
            }
        }
    }
